package com.bykj.taketurns.activity;

import android.content.Context;
import android.content.res.Configuration;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.util.Log;
import android.view.View;

import com.bykj.taketurns.R;
import com.bykj.taketurns.fragment.PlayVideoFragment;
import com.bykj.taketurns.model.VideoShowBean;
import com.bykj.taketurns.widget.ViewPager;
import com.dou361.ijkplayer.listener.OnKeyListener;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.lang.reflect.Type;
import java.util.ArrayList;


public class PlayVideoActivity extends BaseActivity {

    private Context mContext;
    private View rootView;
    private OnKeyListener listenter;
    private ViewPager viewPager;
    private ArrayList<VideoShowBean> list;
    private ArrayList<Fragment> fragments = new ArrayList<>();
    private DrawerLayout mDrawer;
    private ActionBarDrawerToggle mToggle;

    private View mRight;

    protected Fragment curFragment;



    //初始化控件
    public void initView(Bundle savedInstanceState) {
        viewPager = (ViewPager) setViewById(R.id.play_viewpager);


    }


    public void initData(Bundle savedInstanceState) { //获取资源文件中的json文件
        final String fromRaw = getFromRaw();
        //gson解析
        Gson gson = new Gson();
        Type type = new TypeToken<ArrayList<VideoShowBean>>() {
        }.getType();
        list = gson.fromJson(fromRaw, type);
        //添加数据
        for (int i = 0; i < list.size(); i++) {
            PlayVideoFragment fragment = new PlayVideoFragment();
            Bundle bundle=new Bundle();
            bundle.putSerializable("video",list.get(i));
            fragment.setArguments(bundle);
            fragments.add(fragment);
        }
        //适配fragment
        viewPager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int position) {
                return fragments.get(position);
            }

            @Override
            public int getCount() {
                return fragments.size();
            }
        });





    }
    //从raw文件中读取
    public String getFromRaw() {
        String result = "";
        String line = "";
        try {
            InputStreamReader inputReader = new InputStreamReader(getResources().openRawResource(R.raw.date));
            BufferedReader bufReader = new BufferedReader(inputReader);
            while ((line = bufReader.readLine()) != null)
                result += line;

        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }


    public int getParentView() {
        return R.layout.activity_play_video;
    }

    @Override
    public void initToolBar() {

    }
    //加载网络数据
    @Override
    protected void postData() {
    }


    //监听软键盘状态
    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        // Checks whether a hardware keyboard is available
        if (newConfig.hardKeyboardHidden == Configuration.HARDKEYBOARDHIDDEN_NO) {
            Log.d("zzz", "show");
            listenter.onKeyPop();
        } else if (newConfig.hardKeyboardHidden == Configuration.HARDKEYBOARDHIDDEN_YES) {
            listenter.onKeyHide();
            Log.d("zzz", "hide");
        }
    }


    //------------------------------------创建pageradapter内部类-------------------------------------//


    //设置点击监听事件
    @Override
    public void onClick(View view) {

    }
    protected void showFragment(Fragment fragment, int id) {
        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        if (curFragment != null) {
            ft.hide(curFragment);
        }
        if (fragment.isAdded()) {
            ft.show(fragment);
        } else {
            ft.add(id, fragment).show(fragment);
        }
        ft.commitAllowingStateLoss();
        curFragment = fragment;
    }


}
